/**
 * @title AbsListFragment.java
 * @package com.esun.cheetah.view.match.matchlist.fragments
 * @description 抽象的比赛列表fragment类
 * @author liugan
 * @date 2012-7-24
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.match.matchlist.fragments;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.match.matchlist.MatchListScoreCommunicater;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.model.match.matchlist.AbsMatchListSubItemInfo;
import com.esun.cheetah.model.match.matchlist.MatchListScoreSubItemInfo;
import com.esun.cheetah.view.match.matchinfo.MatchInfoAct;
import com.esun.cheetah.view.match.matchlist.adapter.AbsMatchListAdapter;
import com.esun.util.log.EDebug;
import com.esun.cheetah.config.IntentKey;
import com.esun.cheetah.model.match.matchlist.Match;
import com.esun.cheetah.view.custom.PullToRefreshListView;
import com.esun.cheetah.view.custom.PullToRefreshListView.OnRefreshListener;
import com.esun.cheetah.view.match.matchinfo.MatchInfoAct;
import com.esun.cheetah.view.match.matchlist.ScoreActivity;
import com.esun.cheetah.view.match.matchlist.adapter.SeparatedMatchListAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.TextView;

/**
 * @title AbsListFragment
 * @description 抽象的比赛列表界面
 * @author liugan
 */
public abstract class AbsListFragment extends Fragment{

	private PullToRefreshListView mListView;
	protected SeparatedMatchListAdapter mSeparaterMatchListAdapter;
	private ScoreActivity mScoreActivity;
	
	
	protected int mNum;
	private OnMatchClickListener mListener;
	private TextView mEmptyView;		
	
	/**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        mScoreActivity = (ScoreActivity) this.getActivity();
        mSeparaterMatchListAdapter = createListAdapter();
        mListener = new OnMatchClickListener();
        this.setRetainInstance(false);
        
    }
	
   
    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {		
		View view = getMatchListView(inflater,container,savedInstanceState);
		
		mListView = (PullToRefreshListView)view.findViewById(R.id.lv_list_score);
		mListView.setAdapter(mSeparaterMatchListAdapter);
		mListView.setOnRefreshListener(new RefreshMatchListListener());
		mListView.setOnItemClickListener(mListener);
		
		mEmptyView = (TextView)view.findViewById(R.id.tv_match_list_empty);
		
		return view;
	}
	
	
	
	protected void showEmptyText(){
		mEmptyView.setVisibility(View.VISIBLE);
	}
	
	protected void hideEmptyText(){
		if(mEmptyView.getVisibility() == View.VISIBLE){
			mEmptyView.setVisibility(View.INVISIBLE);
	}
	}
	
	protected View getMatchListView(LayoutInflater inflater,ViewGroup container,Bundle saveInstanceState){
		return inflater.inflate(R.layout.match_list_fragment, container,false);
	}
	/**
	 * @title getListAdapter
	 * @description 获取具体类中ListView的Adapter
	 * @return
	 */
	protected abstract SeparatedMatchListAdapter createListAdapter();
	
	protected SeparatedMatchListAdapter getListAdapter(){
		return mSeparaterMatchListAdapter;
	}
	
	/**
	 * @title isNeedUpdate
	 * @description 判断该fragment是否需要刷新
	 * @return
	 */
	public abstract boolean isNeedUpdate();
	
	
	/**
	 * @title update
	 * @description 刷新列表
	 */
	public abstract void update( );
	

	public void checkUpdate(){
		if(isNeedUpdate()){
			update();
		}
	}

    protected void setUpdateTime(String time){
    	mListView.setUpdateTime(time);
    }
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onStart() {		
		super.onStart();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
	}

	
	private class OnMatchClickListener implements OnItemClickListener{

	    	@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(), MatchInfoAct.class);
				Object item = mSeparaterMatchListAdapter.getItem(position);
				if(item instanceof Match){
					Match matchInfo = (Match) mSeparaterMatchListAdapter.getItem(position);
					intent.putExtra(IntentKey.INTENT_KEY_LEAGUE_NAME,
							matchInfo.getLeagueName());
					intent.putExtra(IntentKey.INTENT_KEY_MATCH_ID,
							matchInfo.getMatchId());
					intent.putExtra(IntentKey.INTENT_KEY_LEAGUE_ROUND,
							matchInfo.getRound());
					intent.putExtra(IntentKey.INTENT_KEY_STAGE,
							matchInfo.getStage());
					intent.putExtra(IntentKey.INTENT_KEY_HOST_NUMBER,
							matchInfo.getHomeNumber());
					intent.putExtra(IntentKey.INTENT_KEY_GUEST_NUMBER,
							matchInfo.getAwayNumber());
					getActivity().startActivity(intent);
				}
			}

	    	
	    }
	
	public class RefreshMatchListListener implements OnRefreshListener{

		@Override
		public void onRefresh(boolean bUpdate) {
			mListView.onRefreshComplete();
			mScoreActivity.asynchronizeUpdate();
		}
		
	}

	
	
}
